package net.bitacademy.java22.boundary;

import java.util.ArrayList;

import net.bitacademy.java22.R;
import net.bitacademy.java22.domain.AccessTokenDAO;
import net.bitacademy.java22.service.ProfileHandler;
import net.bitacademy.java22.service.SwHandler;
import net.bitacademy.java22.service.Trigger;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.ComponentName;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.ServiceConnection;
import android.content.pm.ActivityInfo;
import android.os.Bundle;
import android.os.IBinder;
import android.os.RemoteException;
import android.util.Log;
import android.view.KeyEvent;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.widget.Toast;

public class Main extends Activity {
	
	public static ArrayList<Activity> ActivityList = new ArrayList<Activity>();   //activity를 등록한 리스트를 public static 로 정의한다.
	
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        ActivityList.add(this);  //ActivityList에 Main을 추가한다.
        
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
        WebView webView = (WebView)this.findViewById(R.id.webView1);
        webView.setWebViewClient(new PascalWebViewClient(this));
        WebSettings webSettings = webView.getSettings();
        
        webView.setHorizontalScrollbarOverlay(true); //수평
        webView.setVerticalScrollbarOverlay(true); //수직
        
        webSettings.setJavaScriptEnabled(true);
        webView.addJavascriptInterface(new ProfileHandler(this), "ProfileHandler");
        webView.addJavascriptInterface(new JsInterface(), "JsInterface");
        webView.addJavascriptInterface(new AccessTokenDAO(this), "AccessTokenDAO");
        webView.addJavascriptInterface(new Trigger(), "EventImportTrigger");
        webView.addJavascriptInterface(new SwHandler(), "SwHandler");
        webView.loadUrl("file:///android_asset/www/main.html");
              
    }
    
    //Activity 2개를 연결하는 부분입니다.(기준인 Main에서 GoogleLogin을 연결)
    private final class JsInterface {
        public void launchIntent(final String payload) {
        	Main.this.runOnUiThread(new Runnable() {
              public void run() {
            	  Main.this.startActivity(new Intent(Main.this, GoogleLogin.class));
              }
           });
        }
     }    
        
	// back 버튼을 클릭시 종료 할건지에 대해 묻는다
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
    	WebView webView = (WebView)this.findViewById(R.id.webView1);

        if ((keyCode == KeyEvent.KEYCODE_BACK) && webView.canGoBack()) {
        	webView.goBack();
            return true;
        }
        
      if(event.getRepeatCount() == 0)
      {
       // 팝업을 띄움      
       AlertDialog dialog;
          dialog = new AlertDialog.Builder(this).setTitle("PASCal Exit")
              .setMessage("Do you want to quit?")
           .setPositiveButton("Yes", new DialogInterface.OnClickListener() {   
              public void onClick(DialogInterface dialog, int which) {
            	  for (int i = 0; i < Main.ActivityList.size(); i++) {  //실행된 모든 Activity를 부른다.
            		  Main.ActivityList.get(i).finish();  //List가 Static 이므로, Class명.변수명.get으로 접근
            		  }
            	  finish();
              }
             })
              .setNegativeButton("No", new DialogInterface.OnClickListener() {
            
            public void onClick(DialogInterface dialog, int which) {
             dialog.cancel();
            }
           }) 
           .show();
       return true;
      }
        
        return super.onKeyDown(keyCode, event);
    }
  
}
